package cn.xi.service.checkItem;

import cn.xi.checkItem.CheckGroupService;
import cn.xi.dao.CheckGroupDao;
import cn.xi.entity.PageResult;
import cn.xi.entity.QueryPageBean;
import cn.xi.pojo.CheckGroup;
import cn.xi.pojo.CheckItem;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public PageResult findpage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        Page<CheckGroup> findpage =null;
        if (queryString == null || queryString.length() == 0) {
            PageHelper.startPage(currentPage,pageSize);
             findpage = checkGroupDao.findpage();
        }else {
            PageHelper.startPage(currentPage,pageSize);
             findpage = checkGroupDao.findpagewhere(queryString);
            return new PageResult(findpage.getTotal(),findpage.getResult());
        }
        return new PageResult(findpage.getTotal(),findpage.getResult());
    }

    @Override
    public List<CheckItem> findcheckitem() {
       return checkGroupDao.findcheckitem();

    }

    @Override
    public void save(CheckGroup checkGroup,Integer[] checkitemIds) {

        checkGroupDao.save(checkGroup);
        Integer getid = checkGroupDao.getid();
        for (Integer id : checkitemIds) {
            checkGroupDao.saveId(getid,id);
        }

    }

    @Override
    public void update(CheckGroup checkGroup, Integer[] checkitemIds) {

        checkGroupDao.update(checkGroup);
        checkGroupDao.deleteitemandgroup(checkGroup.getId());
        for (Integer id : checkitemIds) {
            checkGroupDao.saveId(checkGroup.getId(), id);
        }

    }

    @Override
    public void delete(Integer id) {
        checkGroupDao.deleteitemandgroup(id);
        checkGroupDao.delete(id);
    }

    @Override
    public HashMap findci(Integer id) {
        HashMap hashMap=new HashMap();
      CheckGroup checkGroup= checkGroupDao.findgroupid(id);
            Integer[] listids=  checkGroupDao.checkitemIds(id);
            hashMap.put("formdata",checkGroup);
            hashMap.put("listid",listids);
        return hashMap;
    }



}
